﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Publishing;

using CSP.Core;

namespace CSP.Receivers
{
    public class ProceduresFeatureReceiver : SPFeatureReceiver
    {

        public void ExcludeFromCurrentNav(string pageID, SPWeb parentWeb)
        {
            parentWeb.AllProperties["__CurrentNavigationExcludes"] = SubstituteNull(parentWeb.AllProperties["__CurrentNavigationExcludes"]) + pageID + ";";
            parentWeb.Update();
        }

        private string SubstituteNull(object o)
        {

            if (o == null)

                return String.Empty;

            else

                return o.ToString();

        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            try
            {
                // feature is scoped at Web, so the parent is type SPWeb rather than SPSite..
                using (SPWeb web = properties.Feature.Parent as SPWeb)
                {
                    PublishingWeb pweb = PublishingWeb.GetPublishingWeb(web);

                    foreach (SPListItem li in pweb.PagesList.Items)
                    {
                        if (li.Name == "case_details.aspx")
                        {
                            ExcludeFromCurrentNav(li.UniqueId.ToString(), web);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CSP.LoggingEngine.Utils.LogError(ex, this.GetType().Name);
            }
        }


        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

    }
}
